import {
	createRouter,
	createWebHashHistory
} from 'vue-router';
import homeChildrenRoutes from "./data"; // 导入 子路由
import Home from '../views/home.vue';

// 定义路由
// 每个路由都需要映射到一个组件。
const routes = [{
		path: "/",
		component: () => import("../views/system/index.vue"),
		redirect: "/login",
		children: homeChildrenRoutes,
		name: "root",
	},
	{
		path: '/login',
		name: 'login',
		component: () => import("../views/login/login.vue"),
	},
	// 你可以继续添加更多的路由规则
];

// 创建路由实例并传递 `routes` 选项
const router = createRouter({
	history: createWebHashHistory(),
	routes, // 缩写，相当于 routes: routes
});


// 导航守卫，检查是否已登录并动态加载路由
router.beforeEach(async (to, from, next) => {
	const token = localStorage.getItem("token") || "";
	if (to.name !== 'login' && !token) {
		next({
			name: 'login'
		});
	} else {
		next();
	}
});

export default router;